package com.itheima.work;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @auther Macrydy
 * @date 2020/7/14 21:39
 */
public class BeanUtils {
    /*
        把map结合中的键值对内容.赋值到obj对象中.
       例如:第一个参数 map中的值是:
              name:tom
              age:19
              sex:男
          传入第二个参数.Student stu

        最后打印stu的值.{ name:tom, age:19, sex:男}
    */
    public void fill(Map<Object,Object> map, Object obj) throws NoSuchMethodException, IllegalAccessException, InstantiationException {
        Class<?> clazz = obj.getClass();
        Object o = clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            fields[i].set(o,map.get(fields[i]));
        }
        System.out.println(o);

    }
}
class Student{
    private String name;
    private int age;
    private String sex;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Student(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Student() {
    }
}
